package pers.dawnyang.modular.system.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pers.dawnyang.common.annotation.OperLog;
import pers.dawnyang.common.domain.Result;
import pers.dawnyang.common.domain.entity.SysRole;
import pers.dawnyang.common.enums.OperType;
import pers.dawnyang.common.enums.ResultEnum;
import pers.dawnyang.common.util.ResultUtil;
import pers.dawnyang.modular.system.domain.request.role.RoleMenuReqU;
import pers.dawnyang.modular.system.domain.request.role.RoleReqC;
import pers.dawnyang.modular.system.domain.request.role.RoleReqR;
import pers.dawnyang.modular.system.domain.request.role.RoleReqU;
import pers.dawnyang.modular.system.domain.response.role.RoleMenuRes;
import pers.dawnyang.modular.system.domain.response.role.RoleRes;
import pers.dawnyang.modular.system.service.RoleService;

/**
 * 角色管理
 * 
 * @author dawn yang
 * 
 */

@RequestMapping("/sys/role")
@Controller
@Validated
public class RoleController {

  @Autowired
  private RoleService roleService;

  @RequiresPermissions("sys:role:index")
  @GetMapping("/index")
  public String index() {
    return "/sys/role/index";
  }

  @RequiresPermissions("sys:role:add")
  @GetMapping("/addView")
  public String addView() {
    return "/sys/role/addView";
  }

  @RequiresPermissions("sys:role:edit")
  @GetMapping("/editView")
  public String editView(@RequestParam("id") Long id, ModelMap model) {
    SysRole sysRole = roleService.getById(id);
    RoleRes roleRes = new RoleRes();
    BeanUtils.copyProperties(sysRole, roleRes);
    model.put("model", JSON.toJSONString(roleRes));
    return "/sys/role/editView";
  }

  @RequiresPermissions("sys:role:edit")
  @GetMapping("/getRoleInfo")
  @ResponseBody
  public Result<RoleRes> getRoleInfo(@RequestParam("id") Long id) {
    SysRole sysRole = roleService.getById(id);
    RoleRes roleRes = new RoleRes();
    BeanUtils.copyProperties(sysRole, roleRes);
    return ResultUtil.success(roleRes);
  }

  @RequiresPermissions("sys:role:list")
  @PostMapping("/getPage")
  @ResponseBody
  public Result<IPage<RoleRes>> getPage(Page<RoleRes> page, RoleReqR req) {
    IPage<RoleRes> iPage = roleService.getPage(page, req);
    return ResultUtil.success(iPage);
  }

  @RequiresPermissions("sys:user:list")
  @GetMapping("/getList")
  @ResponseBody
  public Result<List<SysRole>> getList() {
    LambdaQueryWrapper<SysRole> lqw = new LambdaQueryWrapper<>();
    lqw.eq(SysRole::getRoleStatus, 1);
    List<SysRole> list = roleService.list(lqw);
    return ResultUtil.success(list);
  }

  @OperLog(operModul = "角色管理", operType = OperType.UPDATE)
  @RequiresPermissions("sys:role:edit")
  @PostMapping("/edit")
  @ResponseBody
  public Result<String> edit(@Validated RoleReqU req) {
    boolean flag = roleService.checkRoleLevel(req.getRoleId());
    if (!flag) {
      ResultUtil.error(ResultEnum.ERROR_EXCEEDROLE);
    }
    SysRole sysRole = new SysRole();
    BeanUtils.copyProperties(req, sysRole);
    roleService.updateById(sysRole);
    return ResultUtil.success();
  }

  @OperLog(operModul = "角色管理", operType = OperType.INSERT)
  @RequiresPermissions("sys:role:add")
  @PostMapping("/add")
  @ResponseBody
  public Result<String> add(@Validated RoleReqC req) {
    SysRole sysRole = new SysRole();
    BeanUtils.copyProperties(req, sysRole);
    roleService.save(sysRole);
    return ResultUtil.success();
  }

  @OperLog(operModul = "角色管理", operType = OperType.DELETE)
  @RequiresPermissions("sys:role:del")
  @PostMapping("/del")
  @ResponseBody
  public Result<String> del(@RequestParam("id") Long id) {
    boolean flag = roleService.checkRoleLevel(id);
    if (!flag) {
      ResultUtil.error(ResultEnum.ERROR_EXCEEDROLE);
    }
    SysRole sysRole = new SysRole();
    sysRole.setRoleId(id);
    sysRole.setRoleStatus(0);
    roleService.updateById(sysRole);
    return ResultUtil.success();
  }

  @RequiresPermissions({"sys:role:list", "sys:role:saveRoleMenu"})
  @GetMapping("/getRoleMenu")
  @ResponseBody
  public Result<List<RoleMenuRes>> getRoleMenu(@RequestParam("id") Long id) {
    List<RoleMenuRes> list = roleService.getRoleMenu(id);
    return ResultUtil.success(list);
  }

  /**
   * 保存角色权限菜单
   * 
   * @param req
   * @return
   */
  @OperLog(operModul = "角色管理", operType = OperType.GRANT)
  @RequiresPermissions("sys:role:saveRoleMenu")
  @PostMapping("/saveRoleMenu")
  @ResponseBody
  public Result<String> saveRoleMenu(RoleMenuReqU req) {
    boolean flag = roleService.checkRoleLevel(req.getRoleId());
    if (!flag) {
      ResultUtil.error(ResultEnum.ERROR_EXCEEDROLE);
    }
    roleService.saveRoleMenu(req);
    return ResultUtil.success();
  }
}
